package com.kang.confirm;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: zhangkangkang
 * @Description: 单条消息确认
 * @date 2021/8/3 11:43
 */
public class Confirm1 {
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("tx43", false, false, false, null);
        //开启确认模式
        channel.confirmSelect();
        channel.basicPublish("", "tx43", MessageProperties.TEXT_PLAIN, "zsf".getBytes());
        //发布确认
        //等待发布者确认，确认了才会执行下面的代码，在规定时间内没有确认消息，则会进入else代码
        if (channel.waitForConfirms(2000)) {
            System.out.println("消息被确认");
        } else {
            System.out.println("消息没有被确认");
        }

    }
}
